import logging
from pages.basePage import BasePage
from config.gp_config import GpConfig
from tools.request import Base
from tools.utils import log_time

from tools.logs import Logs

logger = Logs(__name__).get_logger()


class GpZjPage(BasePage):

    def __init__(self, sj_type):
        super().__init__()
        self.request = Base()
        self.type_data = GpConfig.gp_zj_xpath.get(sj_type)

    @log_time
    def get_zj_by_dm(self, dm: str, day: int) -> list:
        """
        根据股票或者板块代码获取指定区间的数据
        :param dm: 代码
        :param day: 日期
        :return: [{"date": "2024-05-20", "spj": "31.08", "zdf": "10.02%", "zj": "2314.45"}]
        """
        # 获取股票数据
        url = self.type_data.get("url").format(dm)
        xpath = self.type_data.get("xpath")
        html = self.get_page_source(url)
        gp_l = []
        for d in range(1, day + 1):
            gp_d = {}
            for k, v in xpath.items():
                gp_d[k] = self.request.parse(html, v.format(d))[0]
            gp_l.append(gp_d)
        return gp_l

    def is_gp_up(self, gp_data: list) -> bool:
        """
        查询当日收盘价是否大于前五日收盘价
        1、如果5日之前是绿的，那么当日收盘价要高于5日最低价
        2、如果5日是红的，那么当日最高价要高于5日收盘价
        :param html:
        :return:
        """
        if self.unit_change(gp_data[-1].get("zdf")) < 0:
            if float(gp_data[0].get("spj")) > float(gp_data[-1].get("spj")):
                return True
        if float(gp_data[0].get("spj")) > float(gp_data[-1].get("spj")):
            return True
        return False

    @staticmethod
    def is_zd_down(gp_data: list) -> bool:
        """
        跌幅连续2天，隔天2点半之后交易，买入红色的
        :param gp_data:
        :return:
        """
        zd_1 = float(gp_data[0].get("zdf").replace("%", ""))
        zd_2 = float(gp_data[1].get("zdf").replace("%", ""))
        zd_3 = float(gp_data[2].get("zdf").replace("%", ""))

        if zd_1 < 0 and zd_2 > 0:
            return True
        if zd_1 < 0 and zd_2 < 0 and zd_3 > 0:
            return True
        return False

    @staticmethod
    def is_jlr(gp_data: list) -> bool:
        """
        判断五日净流入是否正数
        :param gp_data:
        :return:
        """
        sum = 0
        for i in gp_data:
            sum += GpZjPage.unit_change(i.get("zj"))
        if sum > 0:
            return True
        return False

    @staticmethod
    def is_zt(gp_data: list) -> int:
        """
        判断list中是否有大于9.7的值, 用来判断30天内是否有过涨停
        :param gp_data:
        :return: 涨停次数
        """
        count = 0
        for i in gp_data:
            zd_1 = float(i.get("zdf").replace("%", ""))
            if zd_1 > 9.7:
                count += 1
        return count

    def get_gp_tody_zj(self, dm: str):
        """
        获取股票进入资金流入
        :return:
        """
        html = self.get_page_source(self.type_data.get("url").format(dm))
        today_xpath = '//*[@data-field="f62"]/span/text()'
        today_lr = self.request.parse(html, today_xpath)[0]
        return self.unit_change(today_lr)


if __name__ == '__main__':
    gz = GpZjPage("gp")
    bk_zj_data = gz.get_zj_by_dm("002967", 5)
    print(bk_zj_data)
    five_is_ok = gz.gp_zj_is_ok(bk_zj_data, "zj")
    print(five_is_ok)
